<?php
!defined('IN_ROOT') && exit( 'Access Denied' );
class PermissionController extends AdminModuleController 
{
	public function actionSetting() {
		$groupId = getgpc(2);
		
		if($_POST['submitTo']) {
			$permissionList = getgpc('permission', 'P');
			$this->PermissionModel->delete("groupId='$groupId'");
			foreach($permissionList as $v) {
				$this->PermissionModel->insert(array(
					'groupId'=>$groupId,
					'item'=>$v,
				));
			}
			$this->setState('adminItems', NULL);
		}
		
		$permissionList = $this->PermissionModel->findAll("groupId='$groupId'");
		$selected = array();
		foreach($permissionList as $v) {
			$selected[$v['item']] = $v['groupId'];
		}
		$permissions = Lookup::items('permission');
		$i = 1;
		foreach($permissions as $k=>&$v) {
			$sed = 0;
			if (isset($selected[$k])) {
			 	$sed = 1;
			 } 
			$permissions[$k] = array($v, $sed, $i);
			$i++;
		}
		$this->view->assign('groupId', $groupId);
		$this->view->assign('permissions', $permissions);
		$this->display('permission_setting');
	}
}